package cn.xsly.ymcc.web.controller;

import cn.xsly.ymcc.service.IUserCodeService;
import cn.xsly.ymcc.domain.UserCode;
import cn.xsly.ymcc.query.UserCodeQuery;
import cn.xsly.ymcc.result.JSONResult;
import cn.xsly.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userCode")
public class UserCodeController {

    @Autowired
    public IUserCodeService userCodeService;


    /**
     * 生成邀请码
     */
    @RequestMapping(value = "/loadUserCode", method = RequestMethod.GET)
    public String loadUserCode() {

        return userCodeService.loadUserCode();
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody UserCode userCode) {
        if (userCode.getId() != null) {
            userCodeService.updateById(userCode);
        } else {
            userCodeService.insert(userCode);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        userCodeService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(userCodeService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(userCodeService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserCodeQuery query) {
        Page<UserCode> page = new Page<UserCode>(query.getPage(), query.getRows());
        page = userCodeService.selectPage(page);
        return JSONResult.success(new PageList<UserCode>(page.getTotal(), page.getRecords()));
    }
}
